<template>
  <view class="taskCenter">
    <pit-header :title="title"></pit-header>
    <view class="taskCenter-nav">
      <view v-for="(tab,index) in tabs" :key="index" :class="['taskCenter-nav-tab', { active: activeTab === index }] "
        @click="activeTab = index">
        {{tab}}
      </view>
    </view>
    <view class="taskCenter-content">
      <view v-for="(task,taskIndex) in filteredTasks" :key="taskIndex" class="taskCenter-content-list">
        <view v-if="task.isPc" class="pc-tag">电脑端</view>
        <view class="list-title">{{task.title}}</view>
        <view class="list-info">
          <view class="list-info-element">
            <text class="element-text">所属流程：</text>
            <view class="element-value">{{ task.bpmnName }}</view>
          </view>
          <view class="list-info-element">
            <text class="element-text">发起人：</text>
            <view class="element-value">{{ task.startUserName }}</view>
          </view>
          <view class="list-info-element">
            <text class="element-text">发起时间：</text>
            <view class="element-value">{{ task.startTime}}</view>
          </view>
        </view>
        <button v-if="task.canHandle" class="list-btn" @click="handleTask(task)">
          处理任务
        </button>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    apiBpmTaskRunTaskCustomTaskListTotal
  } from '@/api/bpm/task.js'
  export default {
    data() {
      return {
        tabs: ['待办任务', '已办任务', '我发起的'],
        activeTab: 0,
        tasks: [],
        taskType: '',
        // enCode:'',
        title: '任务中心',
        todoCount: 0,
        doneCount: 0,
        initiatedCount: 0
      }
    },
    onLoad(options) {
      if (options && options.type) {
        this.taskType = options.type;
        if (this.taskType === 'todo') this.activeTab = 0;
        else if (this.taskType === 'done') this.activeTab = 1;
        else if (this.taskType === 'initiated') this.activeTab = 2;
      }
      this.init();
    },
    computed: {
      filteredTasks() {
        if (this.activeTab === 0) {
          return this.tasks.filter(task => task.canHandle);
        } else if (this.activeTab === 1) {
          return this.tasks.filter(task => !task.canHandle);
        } else {
          return this.tasks;
        }
      }
    },
    methods: {
      init() {
        apiBpmTaskRunTaskCustomTaskListTotal({
          // category: this.enCode,
          pageSize: 20,
          pageNumber: 1
        }).then(res => {
          if (res.code === '00000') {
            this.tasks = Array.isArray(res.data.list) ? res.data.list : [];
            this.todoCount = this.tasks.filter(task => task.canHandle).length;
            this.doneCount = this.tasks.filter(task => !task.canHandle).length;
            this.initiatedCount = this.tasks.length;
            uni.setStorageSync('taskCounts', {
              todo: this.todoCount,
              done: this.doneCount,
              initiated: this.initiatedCount
            });
          } else {
            uni.showToast({
              title: '数据获取失败',
              icon: 'none'
            });
            this.tasks = [];
          }
        });
      },
    }
  }
</script>

<style lang="scss" scoped>
  .taskCenter {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background-color: rgb(240, 245, 252);

    .taskCenter-nav {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 12px 30px;
      background-color: white;

      .taskCenter-nav-tab {
        font-weight: 550;
        color: rgb(102, 102, 102);
      }

      .taskCenter-nav-tab.active {
        color: #0f519c;
        border-bottom: 2px solid #0f519c;
      }
    }

    .taskCenter-content {
      flex: 1;
      overflow-y: auto;
      padding: 10px;

      .taskCenter-content-list {
        background-color: #fff;
        border-radius: 8px;
        padding: 10px;
        margin-bottom: 10px;

        .pc-tag {
          display: inline-block;
          background-color: #67c23a;
          color: #fff;
          font-size: 12px;
          padding: 2px 5px;
          border-radius: 4px;
          margin-bottom: 5px;
        }

        .list-title {
          // display: flex;
          // align-items: center;
          // .task-date {
          font-size: 18px;
          font-weight: 350;
          // }
          // .task-name {
          //   font-size: 18px;
          //   font-weight: 350;
          // }
        }

        .list-info {
          display: flex;
          flex-direction: column;

          .list-info-element {
            display: flex;
            align-items: center;
            margin: 8px 0;

            .element-text {
              font-size: 16px;
              font-weight: 350;
            }

            .element-value {
              font-size: 16px;
              color: #666;
            }
          }
        }

        .list-btn {
          margin-right: 10px;
          width: 150px;
          font-size: 15px;
          border: none;
          border-radius: 50px;
          color: white;
          background-color: rgb(2, 126, 251);
        }
      }
    }
  }
</style>
